import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Practice2 {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入要复制的文件");
        Scanner scanner =new Scanner(System.in);
        String fileName =scanner.next();
        File file =new File(fileName);
        System.out.println("请输入复制目的地");
        Scanner scanner1 =new Scanner(System.in);
        String desFileName =scanner1.next();
        File desFile =new File(desFileName);
        //因为复制文件进行流的写入操作时如果文件不存在时会创建文件，但如果目录不存在时不知道创建的地方，所以需要先判断目录是否存在
        if (!desFile.getParentFile().isDirectory()){
            System.out.println("目的地目录不存在");
            return;
        }
        if (!file.isFile()){
            System.out.println("这不是文件");
        }else {
           try (InputStream inputStream =new FileInputStream(file);
                OutputStream outputStream=new FileOutputStream(desFile)){
               while (true){
                   byte[] data =new byte[1024];
                   int n=inputStream.read(data);
                   if (n==-1){
                       break;
                   }
                   //不需要传入整个数组，读多少写多少
                   outputStream.write(data,0,n);
               }
           }
        }
    }
}
